#include "QtTutorialApp1.h"
#include <QPainter>

#ifdef WIN32
#ifdef QtTutorial1_EXPORTS
#define QTTUTORIAL1_API __declspec(dllexport)
#else
#define QTTUTORIAL1_API __declspec(dllimport)
#endif
#else
#define QTTUTORIAL1_API
#endif

extern "C" QTTUTORIAL1_API Blobz::QtBaseApplication* GetInstance()
{
	QtTutorial1::QtTutorial1App* pTutorial = new QtTutorial1::QtTutorial1App();
	return pTutorial;
}

extern "C" QTTUTORIAL1_API unsigned int GetMajorReleaseVersion()
{
	return NUI_RELEASE_MAJOR_VER;
}

extern "C" QTTUTORIAL1_API unsigned int GetMinorReleaseVersion()
{
	return NUI_RELEASE_MINOR_VER;
}

extern "C" QTTUTORIAL1_API bool IsQtApp()
{
	return true;
}

extern "C" QTTUTORIAL1_API const char* GetAppName()
{
	return "QtTutorial1";
}

namespace QtTutorial1
{
	/**
	* Constructor
	*/
	QtTutorial1App::QtTutorial1App():
			mNumberOfFingers(0)
	{
	}

	/**
	* Destructor
	*/
	QtTutorial1App::~QtTutorial1App()
	{
	}

	/**
	* Load objects used in the app
	*/
	bool QtTutorial1App::onInitialize()
	{
		// Loads the image
		bool success = mBackground.load(getApplicationDirectory()+"bg.png");

		return success;
	}

	/**
	* Unload all loaded objects
	*/
	void QtTutorial1App::onDeinitialize()
	{
	}

	/**
	* Triggered on every frame update
	* \param inFrameTime Time since last update ( in seconds )
	*/
	void QtTutorial1App::onUpdate( float inFrameTime )
	{
	}

	/**
	* Events handler
	* \param inEvent Consist of event information
	* \return bool
	*/
	bool QtTutorial1App::onEvent( QEvent *inEvent )
	{
		// This program didn't handle the event, so return false
		return false;
	}
	
	/**
	* Triggered when there is a finger down event on the screen
	* \param inData holds the touch information
	*/
	void QtTutorial1App::onFingerDown( const TouchListener::TouchData& inData )
	{
		++mNumberOfFingers;
	}

	/**
	* Triggered when there is a finger up event on the screen
	* \param inData holds the touch information
	*/
	void QtTutorial1App::onFingerUp( const TouchListener::TouchData& inData )
	{
		if(mNumberOfFingers > 0)
			--mNumberOfFingers;
	}

	/**
	* Triggered when there is a finger update event on the screen
	* \param inData holds the touch information
	*/
	void QtTutorial1App::onFingerUpdate( const TouchListener::TouchData& inData )
	{
	}

	/**
	* Triggered when there is a finger moves out of the screen or application window
	* \param inData holds the touch information
	*/
	void QtTutorial1App::onFingerExit( const TouchListener::TouchData& inData )
	{
		// just treat finger exit events as finger up events
		onFingerUp(inData);
	}

	/**
	* Render method
	* \param inpPainter QT Rendering system pointer
	* \param inpOption Pointer to various rendering options
	* \param inpWidget Pointer to Parent Widget
	*/
	void QtTutorial1App::paint( QPainter* inpPainter, const QStyleOptionGraphicsItem *inpOption, QWidget *inpWidget )
	{
		// Save window size for rendering. Getting the window size during rendering ensures that the right size is used.
		QSizeF windowSize = getWindowSize();

		// Draw our previously loaded image, with a height of 20 px and a width of 20 px
		inpPainter->drawImage(QRectF(QPointF(0, 0), windowSize), mBackground); 

		// Set our requested font to Verdana, font size 20
		inpPainter->setFont(QFont("Verdana", 20));
		// Set the brush color to white
		inpPainter->setPen(Qt::black);

		// render the title text
		QString mainText = "Touch the screen and watch the counter increase.";
		QSize textSize = inpPainter->fontMetrics().size(Qt::TextSingleLine, mainText);
		inpPainter->drawText((windowSize.width()-textSize.width())*0.5f, (windowSize.height()-textSize.height())*0.5f - 100, textSize.width(), textSize.height(), Qt::AlignCenter, mainText);

		// render the counter
		QString counterText = "Detected fingers: " + QString::number(mNumberOfFingers);
		textSize = inpPainter->fontMetrics().size(Qt::TextSingleLine, counterText);
		inpPainter->drawText((windowSize.width()-textSize.width())*0.5f, (windowSize.height()-textSize.height())*0.5f, textSize.width(), textSize.height(), Qt::AlignCenter, counterText);
	}
}